app.controller("structure",function($scope,$uibModal,$rootScope,del,$state,get,$stateParams){
	 $scope.selectedFood = {
        	dId:null
        };
    $scope.trees=function(){
	    get.list("department/tree").then(function(e){
			$scope.treedata=e
			   $scope.expandedNodes=[];
			   angular.forEach($scope.treedata,function(o){
			   		$scope.expandedNodes.push(o)
			   		 angular.forEach(o.children,function(os){
			   		 	$scope.expandedNodes.push(os)
			   		 }
			   )
			   })
			})	
    }
	 $scope.trees()
     
        $scope.doSelect = function(node) { 
        	$scope.selectedFood = node;
       		$scope.search()
        };

        $scope.lastRightClickNode = null;
        $scope.doRightClick = function(node) { $scope.lastRightClickNode = node.label; }

        $scope.lastCookedFood = null;
        $scope.doCook = function() { 
        	$scope.lastCookedFood = $scope.selectedFood.name; 
        };
      
        $scope.lastAction = null;
        $scope.doAction = function(cmd) { $scope.lastAction = cmd; };

       /* $scope.isLeaf = function() { return $scope.selectedFood && $scope.selectedFood.children.length == 0;};*/
	$scope.search=function(i){
		var param={
			pageNum:i||'1',
			pageSize:8,
			dId:$scope.selectedFood.dId
		}
		get.list("department/findstaff",param).then(function(e){
			$scope.list=e.list
			$scope.pageSize = e.pageSize; //每页多少条
			$scope.totalNum = e.pageCount; //总共多少页
			$scope.totalSize =e.totalNums; //总共多少条   
			$scope.pageNum = e.pageNum; //当前页面
		})
		
   	
		
	
	}
	
	
   
   $scope.pageUp=function(){//跳转页码
       	if ($scope.tj.page>$scope.totalNum) {
       		$scope.tj.page=$scope.totalNum
       	} else if($scope.tj.page<1){
       	$scope.tj.page=null
       	}
       }//pageUp
   $scope.toPage=function(i){//点击跳转
       i=i||$scope.tj.page
       	$scope.search(i)
       	$scope.tj.current=$scope.tj.page;//页码调整
   }//toPage
   if($stateParams.page!=null){
			$scope.tj={
				page:$stateParams.page
			}
		 $scope.toPage($stateParams.page)
	}else{
			$scope.search();
	}
    /**
	 * 删除tree
	 */
	$scope.modelClickTree = function() { //单删
	$uibModal.open({ //模态框
		animation: true,
		templateUrl: 'myModalDel.html',
		size: 'sm',
		 backdrop: 'static' ,
		controller: function($uibModalInstance) {
			var $ctrl = this;
				$ctrl.state = true;
			$ctrl.ok = function() {
				del.list("department/del",{dId:$scope.selectedFood.dId}).then(function(e){
					$uibModalInstance.close();
					$rootScope.open(e);
					$state.go("home.structure",{page:$scope.pageNum},{
				     		reload:true
				     	})
				});
				
			}; //ok  
			$ctrl.cancel = function() {
				$uibModalInstance.close();
			};
		}, //controller
		controllerAs: '$ctrl'
	})
	}
   /**
	 * 删除
	 */
	$scope.modelClick = function(id) { //单删
		
	$uibModal.open({ //模态框
		animation: true,
		templateUrl: 'myModalDel.html',
		size: 'sm',
		 backdrop: 'static' ,
		controller: function($uibModalInstance) {
			var $ctrl = this;
				$ctrl.state = true;
			$ctrl.ok = function() {
					del.list("staff/del",{sId:id}).then(function(e){
					$uibModalInstance.close();
					$rootScope.open(e);
					$state.go("home.structure",null,{
				     		reload:true
				     	})
				});
				
			}; //ok  
			$ctrl.cancel = function() {
				$uibModalInstance.close();
			};
		}, //controller
		controllerAs: '$ctrl'
	})
	}
	/**
	 * 增加与修改
	 * @param {Object} id
	 * @param {Object} tab
	 */
	$scope.columnAdd = function(id,tab) {
		$uibModal.open({ //模态框
			animation: true,
			templateUrl: 'columnAdd.html',
			size: 'lg',
			 backdrop: 'static' ,
			controller: 'columnAdd',
			resolve: { //这是一个入参,这个很重要,它可以把主控制器中的参数传到模态框控制器中
				id: function() { //是一个回调函数
					return $scope.selectedFood.dId; //这个值会被模态框的控制器获取到
				},
				pid: function() { //是一个回调函数
					return $scope.selectedFood.dId; //这个值会被模态框的控制器获取到
				},
				tab:function (){
					return tab;
				},
				scope:function(){
					return $scope;
				}
			}
		}) //模态框
	}
})
app.controller('columnAdd',function($scope,$rootScope,get,ngTreetableParams,$uibModalInstance,tab,id,$http,post,scope,$state,pid){
	$scope.id=id
	$scope.pid=pid
	if(tab=='add'){
		$scope.sername="添加";
	}else{
		$scope.sername="修改";
		get.list("department/findOne",{dId:$scope.id}).then(function(e){
			$scope.name=e.dName
			$scope.dgDecription=e.dDecription
		})
	}
	if($scope.id==0||$scope.href==''){
		$scope.type="menu"
	}else{
		$scope.type="permission"
	}
	$scope.ok=function (){
		var param={dName:$scope.name||'',
					dDecription:$scope.dgDecription||'',
					dPId:$scope.pid||''
		};
		if(tab=='add'){
			post.list("/department/save",param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			scope.trees()
		});
		}else{
		param.dId=$scope.id
		post.list("/department/update",
			param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			scope.trees()
		});
		}
	}
	$scope.cancel = function() {
		$uibModalInstance.close();
	};
	
});